package com.example.shop.merchant.controller;

import com.example.shop.common.api.ResponseResult;
import com.example.shop.common.api.ServiceReturn;
import com.example.shop.common.api.ServiceStateCode;
import com.example.shop.common.enums.SessionAttributeKey;
import com.example.shop.merchant.service.ChatService;
import com.example.shop.pojo.Merchant;
import com.example.shop.rabbitmq.dto.ChatMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/merchant/chatMessage")
public class ChatMessageController {

    @Autowired
    ChatService chatService;

    @PostMapping(value = "/send",consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult<Boolean> sendChatMessage(HttpSession session, @RequestBody @Validated ChatMessage chatMessage, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return ResponseResult.failure(ServiceStateCode.PARAMETER_NONSTANDARD);
        }
        Merchant merchant = (Merchant) session.getAttribute(SessionAttributeKey.NOW_LOGIN_MERCHANT.getKey());
        ServiceReturn<Boolean> result = chatService.send(merchant, chatMessage);
        return ResponseResult.auto(result.getStateCode(),result.getReturnData());
    }
}
